﻿namespace jzgl.Windows
{
    using jzgl.Controller;
    using jzgl.Model;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Windows.Forms;

    public class FormPicture : Form
    {
        private Button btn_Add;
        private Button btn_Close;
        private Button btn_Delete;
        private Button btnSave;
        private string budget_key;
        private DataGridViewImageColumn clmContent;
        private DataGridViewTextBoxColumn clmDesption;
        private DataGridViewTextBoxColumn clmId;
        private DataGridViewTextBoxColumn clmName;
        private IContainer components;
        private DataGridViewImageColumn dataGridViewImageColumn1;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
        private DataGridView dgvPicture;
        private OpenFileDialog openFileDialog1;
        private Panel panel2;
        private DesPicDB picDB;
        private PictureBox pictureBox1;
        private DataTable table;

        public FormPicture()
        {
            this.components = null;
            this.picDB = new DesPicDB();
            this.table = null;
            this.budget_key = "";
            this.InitializeComponent();
        }

        public FormPicture(string id)
        {
            this.components = null;
            this.picDB = new DesPicDB();
            this.table = null;
            this.budget_key = "";
            this.InitializeComponent();
            this.budget_key = id;
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|All files (*.*)|*.*";
            this.openFileDialog1.FilterIndex = 1;
            this.openFileDialog1.Multiselect = true;
            this.openFileDialog1.RestoreDirectory = true;
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = this.openFileDialog1.FileName;
                string str2 = fileName.Substring(fileName.LastIndexOf(@"\")).ToString().Trim();
                if (this.pictureBox1.Image != null)
                {
                    if (str2.Length > 20)
                    {
                        MessageBox.Show(this, "设计图文件名称长度不能超过20字符\n请修改后再添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                    else
                    {
                        this.pictureBox1.Image = Image.FromFile(fileName);
                        this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                        byte[] buffer = this.getphoto(fileName);
                        DesPic p = new DesPic();
                        p._BudListKey = this.budget_key;
                        p._DesPicName = str2;
                        p.DesPicURL1 = buffer;
                        p._DesPicRemark = "";
                        if (this.picDB.Add_DesPic(p) >= 0)
                        {
                            MessageBox.Show(this, "添加成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            List<DesPic> pic = this.picDB.GetPic(this.budget_key);
                            this.dgvPicture.DataSource = pic;
                            this.dgvPicture.Columns["_BudListKey"].Visible = false;
                        }
                        else
                        {
                            MessageBox.Show(this, "添加失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        }
                    }
                }
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            if ((this.dgvPicture.Rows.Count > 0) && (MessageBox.Show(this, "确定要删除" + this.dgvPicture.CurrentRow.Cells["clmName"].Value.ToString() + "吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))
            {
                DesPic p = new DesPic();
                p._DesPicId = int.Parse(this.dgvPicture.CurrentRow.Cells["clmId"].Value.ToString());
                if (this.picDB.Delete_DesPic(p) >= 0)
                {
                    MessageBox.Show(this, "删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    List<DesPic> pic = this.picDB.GetPic(this.budget_key);
                    this.dgvPicture.DataSource = pic;
                    this.dgvPicture.Columns["_BudListKey"].Visible = false;
                }
                else
                {
                    MessageBox.Show(this, "删除失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            List<DesPic> list = new List<DesPic>();
            foreach (DataGridViewRow row in (IEnumerable) this.dgvPicture.Rows)
            {
                DesPic item = new DesPic();
                item._DesPicId = int.Parse(row.Cells["clmId"].Value.ToString());
                item._DesPicRemark = row.Cells["clmDesption"].Value.ToString();
                list.Add(item);
            }
            if (this.picDB.UpdatePicInfo(list) > 0)
            {
                MessageBox.Show(this, "保存成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            else
            {
                MessageBox.Show(this, "保存失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

        private void dgvPicture_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int id = int.Parse(this.dgvPicture.CurrentRow.Cells[0].Value.ToString());
            Image image = this.picDB.Show_DesPic(id);
            this.pictureBox1.Image = image;
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FormPicture_Load(object sender, EventArgs e)
        {
            List<DesPic> pic = this.picDB.GetPic(this.budget_key);
            this.dgvPicture.DataSource = pic;
            this.dgvPicture.Columns["_BudListKey"].Visible = false;
            this.dgvPicture.Columns["clmDesption"].ReadOnly = false;
        }

        public byte[] getphoto(string photopath)
        {
            string path = photopath;
            FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            stream.Close();
            return buffer;
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(FormPicture));
            this.openFileDialog1 = new OpenFileDialog();
            this.dgvPicture = new DataGridView();
            this.clmId = new DataGridViewTextBoxColumn();
            this.clmName = new DataGridViewTextBoxColumn();
            this.clmDesption = new DataGridViewTextBoxColumn();
            this.clmContent = new DataGridViewImageColumn();
            this.panel2 = new Panel();
            this.btnSave = new Button();
            this.btn_Delete = new Button();
            this.btn_Add = new Button();
            this.btn_Close = new Button();
            this.pictureBox1 = new PictureBox();
            this.dataGridViewTextBoxColumn1 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
            this.dataGridViewImageColumn1 = new DataGridViewImageColumn();
            ((ISupportInitialize) this.dgvPicture).BeginInit();
            this.panel2.SuspendLayout();
            ((ISupportInitialize) this.pictureBox1).BeginInit();
            base.SuspendLayout();
            this.openFileDialog1.FileName = "openFileDialog1";
            this.dgvPicture.AllowUserToAddRows = false;
            this.dgvPicture.AllowUserToOrderColumns = true;
            this.dgvPicture.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            this.dgvPicture.BorderStyle = BorderStyle.Fixed3D;
            this.dgvPicture.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvPicture.Columns.AddRange(new DataGridViewColumn[] { this.clmId, this.clmName, this.clmDesption, this.clmContent });
            this.dgvPicture.Location = new Point(0, 0x139);
            this.dgvPicture.Name = "dgvPicture";
            this.dgvPicture.RowTemplate.Height = 0x17;
            this.dgvPicture.Size = new Size(0x1d0, 0xac);
            this.dgvPicture.TabIndex = 1;
            this.dgvPicture.CellDoubleClick += new DataGridViewCellEventHandler(this.dgvPicture_CellDoubleClick);
            this.clmId.DataPropertyName = "_DesPicId";
            this.clmId.HeaderText = "id";
            this.clmId.Name = "clmId";
            this.clmId.ReadOnly = true;
            this.clmId.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.clmId.Visible = false;
            this.clmName.DataPropertyName = "_DesPicName";
            this.clmName.HeaderText = "图片文件";
            this.clmName.Name = "clmName";
            this.clmName.ReadOnly = true;
            this.clmName.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.clmDesption.DataPropertyName = "_DesPicRemark";
            this.clmDesption.HeaderText = "图片描述";
            this.clmDesption.Name = "clmDesption";
            this.clmDesption.ReadOnly = true;
            this.clmDesption.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.clmContent.DataPropertyName = "DesPicURL1";
            this.clmContent.HeaderText = "内容";
            this.clmContent.Name = "clmContent";
            this.clmContent.ReadOnly = true;
            this.clmContent.Resizable = DataGridViewTriState.True;
            this.clmContent.SortMode = DataGridViewColumnSortMode.Automatic;
            this.clmContent.Visible = false;
            this.panel2.Controls.Add(this.btnSave);
            this.panel2.Controls.Add(this.btn_Delete);
            this.panel2.Controls.Add(this.btn_Add);
            this.panel2.Controls.Add(this.btn_Close);
            this.panel2.Location = new Point(470, 0x13a);
            this.panel2.Name = "panel2";
            this.panel2.Size = new Size(0x7a, 0xac);
            this.panel2.TabIndex = 6;
            this.btnSave.Location = new Point(14, 0x34);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new Size(0x62, 0x22);
            this.btnSave.TabIndex = 5;
            this.btnSave.Text = "保存(&S)";
            this.btnSave.UseVisualStyleBackColor = true;
            this.btnSave.Click += new EventHandler(this.btnSave_Click);
            this.btn_Delete.Location = new Point(14, 0x5c);
            this.btn_Delete.Name = "btn_Delete";
            this.btn_Delete.Size = new Size(0x62, 0x20);
            this.btn_Delete.TabIndex = 3;
            this.btn_Delete.Text = "删除(&D)";
            this.btn_Delete.UseVisualStyleBackColor = true;
            this.btn_Delete.Click += new EventHandler(this.btn_Delete_Click);
            this.btn_Add.Location = new Point(14, 14);
            this.btn_Add.Name = "btn_Add";
            this.btn_Add.Size = new Size(0x62, 0x20);
            this.btn_Add.TabIndex = 2;
            this.btn_Add.Text = "添加(&A)";
            this.btn_Add.UseVisualStyleBackColor = true;
            this.btn_Add.Click += new EventHandler(this.btn_Add_Click);
            this.btn_Close.Location = new Point(14, 130);
            this.btn_Close.Name = "btn_Close";
            this.btn_Close.Size = new Size(0x62, 0x20);
            this.btn_Close.TabIndex = 4;
            this.btn_Close.Text = "关闭(C)";
            this.btn_Close.UseVisualStyleBackColor = true;
            this.btn_Close.Click += new EventHandler(this.btn_Close_Click);
            this.pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
            this.pictureBox1.BorderStyle = BorderStyle.Fixed3D;
            this.pictureBox1.Cursor = Cursors.No;
            this.pictureBox1.Image = (Image) manager.GetObject("pictureBox1.Image");
            this.pictureBox1.Location = new Point(0, 1);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new Size(0x250, 0x135);
            this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            this.dataGridViewTextBoxColumn1.DataPropertyName = "DesPicId";
            this.dataGridViewTextBoxColumn1.HeaderText = "图片文件";
            this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
            this.dataGridViewTextBoxColumn1.ReadOnly = true;
            this.dataGridViewTextBoxColumn1.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dataGridViewTextBoxColumn1.Visible = false;
            this.dataGridViewTextBoxColumn1.Width = 0xf4;
            this.dataGridViewTextBoxColumn2.DataPropertyName = "DesPicName";
            this.dataGridViewTextBoxColumn2.HeaderText = "图片描述";
            this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
            this.dataGridViewTextBoxColumn2.ReadOnly = true;
            this.dataGridViewTextBoxColumn2.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dataGridViewTextBoxColumn2.Width = 0xf4;
            this.dataGridViewTextBoxColumn3.DataPropertyName = "DesPicRemark";
            this.dataGridViewTextBoxColumn3.HeaderText = "id";
            this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
            this.dataGridViewTextBoxColumn3.ReadOnly = true;
            this.dataGridViewTextBoxColumn3.SortMode = DataGridViewColumnSortMode.NotSortable;
            this.dataGridViewTextBoxColumn3.Visible = false;
            this.dataGridViewTextBoxColumn3.Width = 0x94;
            this.dataGridViewImageColumn1.DataPropertyName = "DesPicURL";
            this.dataGridViewImageColumn1.HeaderText = "内容";
            this.dataGridViewImageColumn1.Name = "dataGridViewImageColumn1";
            this.dataGridViewImageColumn1.ReadOnly = true;
            this.dataGridViewImageColumn1.Resizable = DataGridViewTriState.True;
            this.dataGridViewImageColumn1.SortMode = DataGridViewColumnSortMode.Automatic;
            this.dataGridViewImageColumn1.Visible = false;
            this.dataGridViewImageColumn1.Width = 0x95;
            base.AutoScaleDimensions = new SizeF(6f, 12f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x251, 0x1e9);
            base.Controls.Add(this.pictureBox1);
            base.Controls.Add(this.panel2);
            base.Controls.Add(this.dgvPicture);
            base.MaximizeBox = false;
            this.MaximumSize = new Size(0x259, 0x20b);
            this.MinimumSize = new Size(0x259, 0x20b);
            base.Name = "FormPicture";
            this.Text = "家装设计图";
            base.Load += new EventHandler(this.FormPicture_Load);
            ((ISupportInitialize) this.dgvPicture).EndInit();
            this.panel2.ResumeLayout(false);
            ((ISupportInitialize) this.pictureBox1).EndInit();
            base.ResumeLayout(false);
        }
    }
}

